package replaceWords;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MarkDeal {
	
	 public String markdel(String withMark){
		 
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs = null;
			try 
			{
				Class.forName("com.mysql.jdbc.Driver");

				conn = DriverManager
						.getConnection("jdbc:mysql://" + wordHost + "/" + wordDBName + "?user=root");
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select * from marklist");
				while (rs.next()) {
					
					String mark = rs.getString("mark");
					//System.out.println(mark);
					String markSuccedaneum = rs.getString("markSuccedaneum");
					Pattern markPattern = Pattern.compile(mark);
					Matcher markMatcher = markPattern.matcher(withMark);
					withMark = markMatcher.replaceAll(markSuccedaneum);
					/*noMark = rs.getString("mark");
					System.out.println(noMark);*/
				}

			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException ex) {
				// handle any errors
				System.out.println("SQLException: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("VendorError: " + ex.getErrorCode());
			} finally {
				try {
					if(rs != null) {
						rs.close();
						rs = null;
					}
					if(stmt != null) {
						stmt.close();
						stmt = null;
					}
					if(conn != null) {
						conn.close();
						conn = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			//单独处理空格
			Pattern kkPattern = Pattern.compile(" ");
			Matcher kkMatcher = kkPattern.matcher(withMark);
			withMark = kkMatcher.replaceAll("gg1000099hh");
			Pattern kkPattern2 = Pattern.compile("　　");
			Matcher kkMatcher2 = kkPattern2.matcher(withMark);
			withMark = kkMatcher2.replaceAll("gg1000095hh");
			//单独处理英文双引号
			Pattern yinPattern = Pattern.compile("\"");
			Matcher yinMatcher = yinPattern.matcher(withMark);
			withMark = yinMatcher.replaceAll("gg1000098hh");
			//单独处理换行
			Pattern hhPattern = Pattern.compile("\n");
			Matcher hhMatcher = hhPattern.matcher(withMark);
			withMark = hhMatcher.replaceAll("gg1000097hh");
			//单独处理tab
			Pattern tabPattern = Pattern.compile("\t");
			Matcher tabMatcher = tabPattern.matcher(withMark);
			withMark = tabMatcher.replaceAll("gg1000096hh");
			
		 		 return (withMark);
		
	}

	 public String markAdd(String withMark){
		 
			Connection conn = null;
			Statement stmt = null;
			ResultSet rs = null;
			try 
			{
				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager
						.getConnection("jdbc:mysql://" + wordHost + "/" + wordDBName + "?user=root");
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select * from marklist");
				while (rs.next()) {
					
					String mark = rs.getString("mark");
					//System.out.println(mark);
					String markSuccedaneum = rs.getString("markSuccedaneum");
					Pattern markPattern = Pattern.compile(markSuccedaneum);
					Matcher markMatcher = markPattern.matcher(withMark);
					withMark = markMatcher.replaceAll(mark);
					/*noMark = rs.getString("mark");
					System.out.println(noMark);*/
				}

			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException ex) {
				// handle any errors
				System.out.println("SQLException: " + ex.getMessage());
				System.out.println("SQLState: " + ex.getSQLState());
				System.out.println("VendorError: " + ex.getErrorCode());
			} finally {
				try {
					if(rs != null) {
						rs.close();
						rs = null;
					}
					if(stmt != null) {
						stmt.close();
						stmt = null;
					}
					if(conn != null) {
						conn.close();
						conn = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		 //单独处理空格
			Pattern kkPattern = Pattern.compile("gg1000099hh");
			Matcher kkMatcher = kkPattern.matcher(withMark);
			withMark = kkMatcher.replaceAll(" ");
			Pattern kkPattern2 = Pattern.compile("gg1000095hh");
			Matcher kkMatcher2 = kkPattern2.matcher(withMark);
			withMark = kkMatcher2.replaceAll("　　");
			
			//单独处理英文双引号
			Pattern yinPattern = Pattern.compile("gg1000098hh");
			Matcher yinMatcher = yinPattern.matcher(withMark);
			withMark = yinMatcher.replaceAll("\"");
			//单独处理换行
			Pattern hhPattern = Pattern.compile("gg1000097hh");
			Matcher hhMatcher = hhPattern.matcher(withMark);
			withMark = hhMatcher.replaceAll("\n");
			//单独处理tab
			Pattern tabPattern = Pattern.compile("gg1000096hh");
			Matcher tabMatcher = tabPattern.matcher(withMark);
			withMark = tabMatcher.replaceAll("\t");
		 		 return (withMark);	
	}
	 	String wordHost = null;
		String wordDBName = null;
		public void setWordHost(String wordHost) {
			this.wordHost = wordHost;
		}

		public void setWordDBName(String wordDBName) {
			this.wordDBName = wordDBName;
		}
	
}
